<template>
  <el-dialog :visible.sync="visible" title="新建子任务" width="500px"
             @open="openDialog"
             @close="closeDialog">
    <el-row>
      <el-col :span="6">
        <span class="label">选择任务<em style="color: #ff0000">*</em></span>
      </el-col>
      <el-col :span="18">
        <el-select filterable v-model="taskItem.taskId" style="width: 100%"
                   placeholder="请选择">
          <el-option
            v-for="(item,index) in taskList"
            :key="index"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-col>
    </el-row>
    <el-row style="margin-top: 10px">
      <el-col :span="6">
        <span class="label">备注</span>
      </el-col>
      <el-col :span="18">
        <el-input v-model.trim="taskItem.remark"/>
      </el-col>
    </el-row>
    <span class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="submitSelectTask" type="primary">确 定</el-button>
      </span>
  </el-dialog>
</template>

<script>
  export default {
    name: "SelectTaskDialog",
    props: ['knowledgeLibId'],
    data() {
      return {
        visible: false,
        taskList: [],
        taskItem: {
          taskId: '',
          remark: ''
        }
      }
    },
    methods: {
      openDialog() {
        this.taskItem.taskId = ''
        this.taskItem.remark = ''
      },
      closeDialog() {
        this.visible = false
      },
      submitSelectTask() {
        if (!this.taskItem.taskId) {
          this.$message.warning('请选择任务')
          return
        }
        this.$store.commit('setTaskItem', this.taskItem)
        this.closeDialog()
        this.$emit('callBack')
      },
    },
    watch: {
      knowledgeLibId(){
        // 查询当前知识库关联正在进行中的任务, 供用户选择
        this.axios.get('/pmph/action/task/selectTaskInProcessByKnowledgeLibId', {
          params: {
            knowledgeLibId: this.knowledgeLibId,
          }
        }).then((response) => {
          if (response.data.length > 0) {
            this.taskList = response.data
          } else {
            this.$message.warning("绑定知识库没有正在进行的任务, 请先新建任务!")
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
